package com.ludi.basics.controller;

import com.ludi.basics.constants.Constants;
import com.ludi.basics.pojo.entity.User;
import com.ludi.basics.pojo.qo.UserLoginQO;
import com.ludi.basics.pojo.vo.RespResult;
import com.ludi.basics.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author ludi
 * @date:2020/8/20 15:01
 * @description: spring-mvc-basics测试 UserController
 */
@Controller
@RequestMapping("/user")
@Api(tags = "用户业务接口")
public class UserController {
    @Autowired
    private UserService userService;
    //登录功能1测试版
    @ResponseBody
    @RequestMapping(value = "/login",method = RequestMethod.GET)
    @ApiOperation(value = "根据id获取用户信息")
    @ApiImplicitParam(paramType = "query", name = "userId", value = "用户ID"
            , required = true, dataType = "String")
    public User login(@RequestParam String userId) {
        System.out.println("userId:"+userId);
        return new User("小明","666666");
    }
    //登录功能2
    @ResponseBody
    @RequestMapping(value = "/loginByQO",method = RequestMethod.POST)
    @ApiOperation(value = "使用用于QO对象查找用户")
    public RespResult<User> loginByQO(@RequestBody UserLoginQO userLoginQO, HttpServletRequest request) {
        System.out.println(userLoginQO);
        User user = userService.login(userLoginQO);
        //记录user的登陆状态
        request.getSession().setAttribute(Constants.SESSION_KEY_LOGIN_USER,user);
        RespResult<User> respResult = new RespResult<User>();
        respResult.setData(user);
        return respResult;
    }
    //展示用户列表
    @ResponseBody
    @RequestMapping(value = "/show",method = RequestMethod.POST)
    @ApiOperation(value = "展示所有用户列表")
    public RespResult showAll(){
        RespResult<List> respResult = new RespResult<>();
        List<User> list = userService.show();
        respResult.setData(list);
        return respResult;
    }
    //用户注册功能
    @ResponseBody
    @RequestMapping(value = "/register",method = RequestMethod.POST)
    @ApiOperation(value = "用户注册")
    public RespResult<User> register(@RequestBody User user){
        User newUser = userService.register(user.getUserName(), user.getPassword());
        RespResult<User> respResult = new RespResult<User>();
        respResult.setData(newUser);
        return respResult;
    }

}
